package com.brk.socket.util;

import java.util.Random;

/**
 * @author brk
 * @date 2023/7/17 14:10
 * @Description:
 */
public class VerificationCode {

    // 可自定义验证码字符源
    private static final String VERIFY_CODES = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 使用系统默认字符源生成验证码
     *
     * @return
     */
    public static String generateVerifyCode() {
        return generateVerifyCode(VERIFY_CODES);
    }

    /**
     * 使用指定源生成验证码
     *
     * @param sources 验证码字符源
     * @return
     */
    public static String generateVerifyCode(String sources) {
        if (sources == null || sources.length() == 0) {
            sources = VERIFY_CODES;
        }
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder();
        /*生成长度为4得验证码*/
        for (int i = 0; i < 4; i++) {
            verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
        }
        return verifyCode.toString();
    }



}
